<?php
$time = mktime();

//--------------LOG PROCES
$filename = 'logProcess';
$somecontent = "-------------------";
$somecontent .= "Acces ({$time}):";
$somecontent .= print_r($_SERVER,1);
$somecontent .= print_r($_GET,1);

if (is_writable($filename)) {
    if (!$handle = fopen($filename, 'a')) {
        echo "no se puede abrir el archivo ($filename)";
        exit;
    }

    // escribimos $somecontent en el archivo abierto.
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "no se puede escribir en el archivo ($filename)";
        exit;
    }

    fclose($handle);
}
//--------------FI LOG PROCES

$Clau = "WeLoveCreativityAndProducction";

$hash = sha1($time.$Clau);

if ($hash == sha1($time.$Clau)) {    
    include_once '../../config.php';

    $bResult = false;
    $aRes = array(
        'result' => -1
    );

    $oUser = new User();
    $rs = array();
    if ($oUser->isLogged()){
        switch ($_GET["action"]) {

            case "team_info":
        	$oTeam = $oUser->getTeam();
                print "playerid;TEAM_A&none;{$oUser->getUserId()};\r";
                print str_replace("{TID}", "A", $oTeam->getTeamData("unity"));
        	break;

            case "tuto_info":
                //TODO: modificar chapuza xD
        	$oTeam = $oUser->getTeam();
                print "playerid;TEAM_A&none;{$oUser->getUserId()};\r";
                print "matchid;GAME;1_2;\r";
                print "server_ip;GAME;192.168.156.76;\r";
                print "myId;GAME;1;\r";
                print "match_dbid;GAME;1234;\r";
                print str_replace("{TID}", "A", $oTeam->getTeamData("unity"));

                print "playerid;TEAM_B&none;{$oUser->getUserId()};\r";
                print str_replace("{TID}", "B", $oTeam->getTeamData("unity"));
        	break;
        	
            case "match_info":
                $oRanking = new Ranking();
                print "fullScreen;GAME;1;\r"; //TODO
                print $oRanking->getMatchData($oUser->getUserId());
                break;

            case "match_end":
                $res = $_GET["res"];
                $idMatch = $_GET["idMatch"];
                $idJ2 = $_GET["idJ2"];
                if (($res == 1 OR $res == 2) AND is_numeric($idJ2) AND is_numeric($idMatch)) { //1 guanya, 2 perd
                    $oRanking = new Ranking();
                    if ($oRanking->endMatch($idMatch, $oUser->getUserId(), $idJ2, $res)) {
                        
                        $aRes["result"] = 0;
                    }
                    else {
                        $aRes["result"] = 1;
                        $aRes["errors"][] = "Error al finalizar la partida.";
                    }
                    
                }
                else {
                    $aRes["result"] = 1;
                    $aRes["errors"][] = "Codigo de resultado, o identificador de jugador, incorrectos.";
                }
                echo json_encode($aRes);
                break;

            case "match_replay":
                $idMatch = $_GET["idMatch"];
                $idJ2 = $_GET["idJ2"];
                 if (is_numeric($idJ2) AND is_numeric($idMatch)) {
                    $oRanking = new Ranking();
                    $idPartida = $oRanking->replayMatch($idMatch, $oUser->getUserId(), $idJ2);
                    if ($idPartida) {
                        $sMatchData = "####################\r";
                        $sMatchData .= "# General Game Data\r";
                        $sMatchData .= "####################\r\r";
                        $sMatchData .= "match_dbid;GAME;{$idPartida};\r";
                        print $sMatchData;
                    }
                    
                } else {
                    $aRes["result"] = 1;
                    $aRes["errors"][] = "Identificador de jugadores, incorrectos.";
                    echo json_encode($aRes);
                }
                break;

            case "match_logout":
                $idMatch = $_GET["idMatch"];
                $idJ2 = $_GET["idJ2"];
                 if (is_numeric($idJ2) AND is_numeric($idMatch)) {
                    $oRanking = new Ranking();
                    $oRanking->logoutMatch($idMatch, $oUser->getUserId(), $idJ2);
                    $aRes["result"] = 0;
                } else {
                    $aRes["result"] = 1;
                    $aRes["errors"][] = "Identificador de jugadores, incorrectos.";
                }
                break;
/*
            case "playerinfo":
                $id_user = $oUser->getUserId();
                if (isset($_GET["id_user"])) $id_user = $_GET["id_user"];                
                if ($aRes["user"] = $oUser->getUserDataUnity($id_user, array(false, true, false, false))) $aRes["result"] = 0;
                else $aRes["result"] = 1;
                echo "result=" . $aRes["result"];
                echo "user=".$aRes["user"];
                //echo $oUser->getUserDataUnity($id_user, array(false, true, false, false));              
                break;

            case "user_id":
                $aRes["result"] = 0;
                $aRes["user_id"] = $oUser->getUserId();
                echo json_encode($aRes);
                break;
*/
            default:
                $aRes["result"] = 4;
                echo json_encode($aRes);
        }
    }
    else {
        $aRes["result"] = 1;
        $aRes["errors"] = $oUser->getmyFbErrors();
        $aRes["errors-handler"] = "facebook";
        echo json_encode($aRes);
    }

    
    //print_r($aRes);
}
?>
